<?php

/**
 * Gestion des catégories
 */
class Magasin extends SimpleContentManager
{
    // Attributs
    const COLLECTION = '6';

    /**
     * Constructeur
     */
    public function __construct()
    {
        $this->_collection = self::COLLECTION;
        parent::__construct();
    }

    /**
     * Retourne les n magasins moins chers
     * @param int $limit
     * @return array
     */
    public function getMoinsChers($limit = 10)
    {
        $datas = $this->orderByPrix();

        $return = array();
        $i = 0;
        foreach($datas as $key => $d){
            if($i < $limit){
                $return[$key] = $d;
            }else
                break;
            $i++;
        }
        return $return;
    }

    /**
     * retourne les n magasins plus chers
     * @param int $limit
     * @return array
     */
    public function getPlusChers($limit = 10)
    {
        $datas = $this->orderByPrix();
        $datas = array_reverse($datas);
        $return = array();
        $i = 0;
        foreach ($datas as $key => $d) {
            if ($i < $limit) {
                $return[$key] = $d;
            } else {
                break;
            }
            $i++;
        }
        return $return;
    }

    /***
     * Trie les magasins en fonction des prix moyen constaté
     * @return array
     */
    public function orderByPrix()
    {
        $shops = $this->getAll();
        $magasins = null;

        if (is_array($shops)) {
            foreach ($shops as $shop) {
                $n_total = $n_nb = $note = 0;
                $sID = (string)$shop['_id'];
                $magasins[$sID]['name'] = $shop['name'];
                $products = Base::Load(CLASS_RELEVE)->findBy('magasin', $sID);
                if (is_object($products)) {
                    foreach ($products as $product) {
                        if (!empty($product['n_total'])) {
                            $n_total += $product['n_total'];
                            $n_nb += $product['n_nb'];
                        }
                    }
                    $note = $n_total / $n_nb;
                }
                $magasins[$sID]['note'] = $note;
            }
        }
        unset($shops);
        $tmp = array();
        foreach ($magasins as $k => $m) {
            $tmp[$m['note']][$k]['name'] = $m['name'];
        }
        unset($magasins);

        // tri par ordre de prix
        ksort($tmp);
        $tmp2 = array();
        foreach ($tmp as $note => $t) {
            foreach($t as $key => $t2){
                $tmp2[$key] = $t2;
                $tmp2[$key]['note'] = $note;
            }             
        }

        return $tmp2;
    }

    public function getAllTransform($villeID = false)
    {
        if (!$villeID) {
            $data = parent::getAll();
        } else {
            $data = (array)$this->findBy('ville', (string) $villeID);
        }

        foreach ($data as $k => $d) {
            switch ($d['type']) {
                case '0':
                    $data[$k]['type'] = 'Autres';
                break;
                case '1':
                    $data[$k]['type'] = 'Supermarché';
                break;
                case '2':
                    $data[$k]['type'] = 'Hypermarché';
                break;
                case '3':
                    $data[$k]['type'] = 'Supérette';
                break;
            }
        }
        return $data;
    }
}


/* -- Fin du fichier -- */